package cn.kgc.faqmanager.controller;

import cn.kgc.faqmanager.entity.ResultEntity;
import cn.kgc.faqmanager.pojo.Faqs;
import cn.kgc.faqmanager.service.FaqsService;
import com.github.pagehelper.PageInfo;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;

/**
 * @author shkstart
 * @create 郭祥伟
 */
@RestController
public class FaqsController {
    @Resource
    FaqsService faqsService;
    @RequestMapping("/selectFaqs")
    public ResultEntity<PageInfo<Faqs>> selectFaqs(
            @RequestParam(value = "classId",required = false,defaultValue = "0") Integer classId,
            @RequestParam(value = "pageNum",required = false,defaultValue = "1") Integer pageNum,
            @RequestParam(value = "pageSize",required = false,defaultValue = "4") Integer pageSize
    ){
        PageInfo<Faqs> pageInfo=faqsService.select(pageNum,pageSize, classId);
        return ResultEntity.successWithoutData(pageInfo);
    }

    //添加和修改
    @RequestMapping("/saveFaqs")
    public ResultEntity<String> saveFaqs(@RequestBody Faqs faqs){
        try {
            Integer integer=0;
            if (faqs.getId() == null) {
                //添加
                integer = faqsService.add(faqs);
            } else {
                //修改
                integer = faqsService.update(faqs);
            }
            return ResultEntity.successWithoutData(integer+"");
        }catch (Exception ex) {
            return ResultEntity.failed(ex.getMessage());
        }
    }

    //根据id查询
    @RequestMapping("/getFaqsById")
    public ResultEntity<Faqs> getFaqsById(Integer id){
        Faqs faqs=faqsService.selectById(id);
        return ResultEntity.successWithoutData(faqs);
    }
    //根据id删除
    @RequestMapping("/delFaqsById")
    public ResultEntity<String> delFaqsById(Integer id){
        try{
            Integer integer=faqsService.delete(id);
            return ResultEntity.successWithoutData(integer+"");
        }catch (Exception ex){
            return ResultEntity.failed(ex.getMessage());
        }

    }
}
